package com.xsoft.demo12.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.xsoft.demo12.GoodsInfo;
import com.xsoft.demo12.R;
import com.xsoft.demo12.db.ShopDataBaseHelper;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class ShopsListFragment extends Fragment implements OnPriceListener {
    private TextView function;
    private RecyclerView recyclerView;
    private TextView count;
    private TextView price;
    private Button delete;
    private float totalPrice;
    private int totalCount;
    private List<GoodsInfo> goodsInfos = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shop_list, container, false);
        view.findViewById(R.id.repeat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchAdapter();
            }
        });
        function = (TextView) view.findViewById(R.id.function);
        function.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (delete.getVisibility() == View.VISIBLE) {
                    delete.setVisibility(View.GONE);
                    function.setText("管理");
                } else {
                    delete.setVisibility(View.VISIBLE);
                    function.setText("完成");
                }
            }
        });
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        count = (TextView) view.findViewById(R.id.count);
        price = (TextView) view.findViewById(R.id.price);
        delete = (Button) view.findViewById(R.id.delete);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new ShopDataBaseHelper(getContext()).delete(goodsInfos);
                for (GoodsInfo info : goodsInfos) {
                    onPrice(info.getPrice() * -1);
                }
                if (recyclerView.getAdapter() instanceof ShopGridAdapter) {
                    ((ShopGridAdapter) recyclerView.getAdapter()).bindData(new ShopDataBaseHelper(getContext()).query());
                } else {
                    ((ShopsListAdapter) recyclerView.getAdapter()).bindData(new ShopDataBaseHelper(getContext()).query());
                }
            }
        });
        initListAdapter();
        return view;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden && isAdded() && recyclerView != null) {
            if (recyclerView.getAdapter() instanceof ShopGridAdapter) {
                ((ShopGridAdapter) recyclerView.getAdapter()).bindData(new ShopDataBaseHelper(getContext()).query());
            } else {
                ((ShopsListAdapter) recyclerView.getAdapter()).bindData(new ShopDataBaseHelper(getContext()).query());
            }
        }
    }

    private void switchAdapter() {
        totalPrice = 0;
        if (recyclerView.getAdapter() instanceof ShopGridAdapter) {
            initListAdapter();
        } else {
            initGridAdapter();
        }
    }

    private void initListAdapter() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),
                LinearLayoutManager.VERTICAL, false));
        ShopsListAdapter adapter = new ShopsListAdapter();
        adapter.setOnPriceListener(this);
        adapter.bindData(new ShopDataBaseHelper(getContext()).query());
        recyclerView.setAdapter(adapter);
    }

    private void initGridAdapter() {
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        ShopGridAdapter adapter = new ShopGridAdapter();
        adapter.setOnPriceListener(this);
        adapter.bindData(new ShopDataBaseHelper(getContext()).query());
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onPrice(float price) {
        totalPrice += price;
        if (price < 0) {
            totalCount--;
        } else if (price > 0) {
            totalCount++;
        }
        this.count.setText("已选个数：" + totalCount);
        this.price.setText("合计：¥" + new DecimalFormat("0.00").format(totalPrice));
    }

    @Override
    public void onChecked(GoodsInfo info, boolean checked) {
        if (checked) {
            goodsInfos.add(info);
        } else {
            goodsInfos.remove(info);
        }
    }
}
